$(function(){
    "use strict";
    $.ajax({
        url:'/Admin/Role/addRole',
        type:'get',
        success:function(data){
            if(data.code==1){
                let datas = data.all_rules
                let htmls = ''
                for(let i=0;i<datas.length;i++){
                    htmls += `
                            <li class="big_power_list">
                                <p class="checkbox_list">
                                    <input type="checkbox" value="${datas[i].id}" id="${datas[i].id}"/>
                                    <label for="${datas[i].id}">${datas[i].rule_name}</label>
                                </p>

                    `
                    htmls += `<ul class="small_power_list">`
                    for(let r =0;r<datas[i].child_list.length;r++){
                        htmls += `
                            <li class="checkbox_list">
                                <input type="checkbox" value="${datas[i].child_list[r].id}" id="${datas[i].child_list[r].id}"/>
                                <label for="${datas[i].child_list[r].id}">${datas[i].child_list[r].rule_name}</label>
                            </li>
                        `
                    }
                    htmls += `</ul></li>`
                }
                $('#ajax_power_list').html(htmls)
            }
        }
    })
    let href = window.location.href;
    let hrefINdex = href.indexOf('=');
    let edit_id
    if(hrefINdex>0){
        edit_id = href.substring(hrefINdex+1);
    }

    // console.log(edit_id)
    if(edit_id){
        $.ajax({
            url:'/Admin/Role/editRole',
            type:'get',
            data:{
                id:edit_id
            },
            success:function(data){
                let name = data.data.role_name
                let powerList = data.rule_ids
                $('#add_name').val(name)
                let checkBoxs = $('input[type="checkbox"]')
                for(let p = 0; p < powerList.length; p++){
                    for(let ci = 0; ci < checkBoxs.length; ci++){
                        if($(checkBoxs[ci]).attr('id')== powerList[p].rule_id){
                            checkBoxs[ci].checked = true
                        }
                    }
                }
            }
        })
    }

    $('#power_save_btn').click(function(e){
        let name = $('#add_name').val()
        let power_list = []
        let checkBox_list = $('input[type="checkbox"]')
        for(let j = 0;j<checkBox_list.length;j++){
            if(checkBox_list[j].checked){
                power_list.push($(checkBox_list[j]).val())
            }
        }
        //防止重复点击按钮
            if( $(this).attr('lock') ) return
            $(this).attr('lock','lock')

        let that = this

        if(name && power_list.length>0 && !edit_id){

            $.ajax({
                url:'/Admin/Role/addRole',
                type:'post',
                data:{
                    role_name: name,
                    rule_ids: power_list
                },
                success:function(data){
                    if(data.code==1){
                       console.log(data)
                        window.location.href='/Admin/Role/index';
                    }else{

                    }
                    $(that).removeAttr('lock')
                }
            })
        }else if( name && power_list.length>0 && edit_id ){
            $.ajax({
                url:'/Admin/Role/editRole',
                type:'post',
                data:{
                    id:edit_id,
                    role_name: name,
                    rule_ids: power_list
                },
                success:function(data){
                    if(data.code==1){
                        console.log(data)
                        // window.location.href = 'lu_ManagementGroup.html'
                        // window.sessionStorage.setItem('edit_id','');
                        window.location.href='/Admin/Role/index';
                    }else{

                    }
                    $(that).removeAttr('lock')
                }
            })
        }else{
            $('#yz_modal').html('您还没有填写管理组名称或选择权限!')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
    })




    //全选的功能
    $('#ajax_power_list').on('click','.small_power_list>li input[type="checkbox"]',function(e){
        let checkboxs =$('#ajax_power_list .small_power_list>li input[type="checkbox"]')
        let that = this
        let n = 0
        // let check = this.checked
        // console.log(check)

        for (let k = 0; k < checkboxs.length; k++) {
            if (checkboxs[k].checked) {
                n++
                // check = true
                // $(that).closest('.big_power_list').children('p').find('input')[0].checked = this.checked
            }
        }
        if(n >= 1){
            console.log('hhh')
            $(that).closest('.big_power_list').children('p').find('input')[0].checked = true
        }
    })
    $('#ajax_power_list').on('click','p input[type="checkbox"]',function(e){
        // let 
        if(!this.checked){
            let checkboxs = $(this).parent().siblings().children('li').find('input[type="checkbox"]')
            for(let c=0;c<checkboxs.length;c++){
                let checkbox=checkboxs[c];
                checkbox.checked=false;
            }
        }else{

        }
    })

    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }

})